import { jobGoto } from "./goto";
import { jobTransfer } from "./transfer";

export const jobSuicide = {
    /**@param {Creep} creep */
    run(creep){
        jobTransfer.run(creep);
        
        if(creep.store.getUsedCapacity() === 0){
            try{
                let spawn = Game.rooms[creep.room.memory.mainRoomName].find(FIND_MY_SPAWNS,{
                    filter(spawn){
                        spawn.store.getFreeCapacity()>0;
                    }
                })[0];
                let ret = spawn.recycleCreep(creep);
                if(ret === ERR_FULL){
                    throw new Error();
                }
                if(ret === ERR_NOT_IN_RANGE){
                    jobGoto.run(creep, [spawn]);
                }
            }catch(e){
                creep.suicide();
            }
        }

        return OK;
    }
}